Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITextView属性字符串的颜色和字体应设置在哪里_Ios_Uitextview_Nsattributedstring - Fatal编程技术网

Ios UITextView属性字符串的颜色和字体应设置在哪里

Ios UITextView属性字符串的颜色和字体应设置在哪里,ios,uitextview,nsattributedstring,Ios,Uitextview,Nsattributedstring,当我在故事板中使用UITextView时,我可以选择文本作为属性字符串,并设置其属性,如颜色、字体和其他属性。当我在代码中设置textView.text=@“我的字符串”时,它会在我设置时显示出来 我的问题是,如果我想在代码中这样做,那么这个情节提要属性设置的等价物是什么?从文档中,我看到了一些属性,如textColor、font,但缺少一些可以设置的属性,如line height。这是通过属性字符串对象完成的!您一定要看一下编程指南: 根据要求,提供一个简短示例,演示如何创建属性化字符串 -

当我在故事板中使用UITextView时,我可以选择文本作为属性字符串,并设置其属性,如颜色、字体和其他属性。当我在代码中设置
textView.text=@“我的字符串”
时,它会在我设置时显示出来


我的问题是,如果我想在代码中这样做,那么这个情节提要属性设置的等价物是什么?从文档中,我看到了一些属性,如textColor、font,但缺少一些可以设置的属性,如line height。

这是通过属性字符串对象完成的!您一定要看一下编程指南:

根据要求,提供一个简短示例,演示如何创建属性化字符串

- (void)viewDidLoad
{
    [super viewDidLoad];

    //
    // Example 1: Create attributed string and set attributes for ranges
    //
    NSString *plainString = @"Hello World";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:plainString];

    // change the font of the entire string
    [attributedString addAttribute:NSFontAttributeName
                             value:[UIFont fontWithName:@"Courier New" size:16.0]
                             range:NSMakeRange(0, plainString.length)];

    // set the font color to blue for the word 'World'
    [attributedString addAttribute:NSForegroundColorAttributeName
                             value:[UIColor blueColor]
                             range:NSMakeRange(6, 4)];
    // assign to texfield 1
    self.tfAttributedString1.attributedText = attributedString;

    //
    // Example 2: Create attributed string based on html
    //
    NSString *htmlString = @"<p style='font-family:Courier New'>Hello <span style='color:blue'>html</span></p>";
    NSData *htmlStringData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *attributedStringOptions = @{
                                              NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
                                              NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)
                                              };
    // assign to texfield 2
    self.tfAttributedString2.attributedText = [[NSAttributedString alloc] initWithData:htmlStringData
                                                                               options: attributedStringOptions
                                                                    documentAttributes:nil error:nil];
}
-(void)viewDidLoad
{
[超级视图下载];
//
//示例1:创建属性字符串并设置范围的属性
//
NSString*plainString=@“你好世界”;
NSMUTABLeAttributeString*AttributeString=[[NSMUTABLeAttributeString alloc]initWithString:plainString];
//更改整个字符串的字体
[attributedString addAttribute:NSFontAttributeName
值:[UIFont fontWithName:@“Courier New”大小:16.0]
范围:NSMakeRange(0,纯字符串.length)];
//将单词“World”的字体颜色设置为蓝色
[attributedString addAttribute:NSForegroundColorAttributeName
值:[UIColor blueColor]
范围:NSMakeRange(6,4)];
//分配给texfield 1
self.tfAttributedString1.attributedText=attributedString;
//
//示例2:基于html创建属性字符串
//
NSString*htmlString=@“

Hello html

”; NSData*htmlStringData=[htmlString DATAUSINGENCODE:NSUTF8StringEncoding]; NSDictionary*attributedStringOptions=@{ NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding) }; //分配给texfield 2 self.tfAttributedString2.attributedText=[[NSAttributedString alloc]initWithData:htmlStringData 选项:attributedStringOptions 文档属性:无错误:无]; }
结果是这样的:


在IB中可以做什么,都可以在代码中完成。在这种情况下,您可以使用
attributedText
属性来设置IB中可以设置的内容。但是如果在整个文本中使用相同的样式,我认为最好只使用基本配置,如
textColor
。我不知道线的高度。但是如果可以在IB中设置,您可以将其设置为
attributedText
。对于更高级的控制,有。这不是一个有用的答案。通过显示一些相关的代码来改进它,这些代码显示了它是如何完成的。否则,删除答案并将其作为评论发布。不,不是。他在想,这是通过向UITextView或其他东西发送消息来完成的,但事实并非如此——它是通过AttributedString对象方法完成的。如果你点击我所指的参考资料,苹果提供的页面顶部总是有一个最新的例子。为什么您认为最好将此代码段复制并粘贴到此处,使其不再更新?因为只有链接的答案通常会被删除。通过显示有用的细节,而不仅仅是一个链接,可以更好地提供答案。它应该展示如何创建NSAttributedString以及如何将其应用于文本视图。我站在他一边,除了他的评论之外,你应该尽量不要这样做来限制机会。链接腐烂随着互联网生态系统的发展而增长。包含链接没什么错,只是应该用作参考,而不是回答。伙计们,我更新了我的答案,并加入了一个简短的例子作为参考。