UITextView 我应该在哪里为属性字符串设置颜色和字体 [英] UITextView where should I set color and font for for attributed string

查看:34
本文介绍了UITextView 我应该在哪里为属性字符串设置颜色和字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I use UITextView in storyboard, I can select text as attributed string and set its attributes like color, font and other attributes. And when I set textView.text = @"my string" in code it show up as I set.

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

My question is if I want to do this in code, what would be equivalent to this storyboard attributes setting? From document https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/index.html#//apple_ref/occ/instp/UITextView/text I see some property i.e. textColor, font, but lack of some property that I can set like line-height.

推荐答案

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

This is done via the attributed strings object! You should definitely have a look at the programming guide: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html

应要求提供一个简短示例,说明如何创建属性字符串.

Upon request, a short example to show how you create an attributed string.

- (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 我应该在哪里为属性字符串设置颜色和字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆