【问题标题】:UITextView where should I set color and font for for attributed stringUITextView 我应该在哪里为属性字符串设置颜色和字体
【发布时间】:2015-02-18 18:21:53
【问题描述】:

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

我的问题是,如果我想在代码中执行此操作,那么这个情节提要属性设置等效于什么?从文档 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/index.html#//apple_ref/occ/instp/UITextView/text 我看到一些属性,即 textColor、字体,但缺少一些我可以设置的属性,例如 line-height。

【问题讨论】:

  • 在 IB 中能做的,都可以用代码来做。在这种情况下,您可以使用attributedText 属性来设置您在 IB 中可以设置的内容。但是如果你在整个文本中使用相同的样式,我认为只使用像textColor 这样的基本配置会更好。我不知道行高。但是,如果您可以在 IB 中设置它,则可以将其设置为 attributedText。如需更高级的控制,有Text Kit

标签: ios uitextview nsattributedstring


【解决方案1】:

这是通过属性字符串对象完成的!你绝对应该看看编程指南: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html

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

- (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];
}

这就是结果的样子:

【讨论】:

  • 这不是一个有用的答案。通过显示一些相关的代码来改进它,展示它是如何完成的。否则删除答案并将其作为评论发布。
  • 不,不是。他在想,这是通过向 UITextView 或其他东西发送消息来完成的,但事实并非如此——它是由 AttributedString 对象方法完成的。如果您单击我指向的参考资料,Apple 提供的页面顶部有一个始终是最新的示例。为什么您认为将这个 sn-p 复制并粘贴到这里会更好?
  • 因为只有链接的答案通常会被删除。通过显示有用的细节,而不仅仅是一个链接,答案会更好。它应该展示如何创建一个 NSAttributedString 以及如何将它应用到文本视图。
  • Eike @rmaddy 有一个很好的观点。我站在他一边,除了他的 cmets,你应该尽量不要这样做来限制link rot 的机会。链接腐烂随着互联网生态系统的发展而增长。包含链接没有错,它应该用作参考而不是答案
  • 好的。伙计们,我更新了我的答案,并附上了一个简短的例子作为参考。
猜你喜欢
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多