【问题标题】:How to load txt file to UITextView with attributedString?如何使用属性字符串将 txt 文件加载到 UITextView?
【发布时间】:2015-11-26 06:58:33
【问题描述】:

我在捆绑包中添加了一个 .txt 文件,该文件包含五个 段落,每个段落都有一个标题。我成功加载了那个 .txt 文件到一个字符串变量,并在我的UITextView 中显示文本。但是我 希望每个标题都有不同的字体,每个段落都有 不同的字体大小。请帮帮我。

我正在使用以下代码:

NSString *path = [[NSBundle mainBundle]pathForResource:@"License" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.textView setText:content];

【问题讨论】:

    标签: ios objective-c nsmutableattributedstring


    【解决方案1】:

    您可以使用NSAttributedString,设置文本字体、前景色和背景色、StrikeThrough 和Shadow 等。

    属性字符串在字符及其属性之间建立关联。与 NSString 对象一样,也有两种变体,NSAttributedString 和 NSMutableAttributedString。虽然之前的 iOS 版本支持属性字符串,但直到 iOS 6 之后,按钮、标签、文本字段和文本视图等控件才定义了一个属性来管理属性。属性应用于一系列字符,例如,您可以为字符串的一部分设置删除线属性。还需要注意的是,属性字符串对象的默认字体是 Helvetica 12-point。如果您为完整字符串以外的范围设置字体属性,请记住这一点。可以使用属性字符串设置以下属性:

    NSString *const NSFontAttributeName;
    NSString *const NSParagraphStyleAttributeName;
    NSString *const NSForegroundColorAttributeName;
    NSString *const NSBackgroundColorAttributeName; 
    NSString *const NSLigatureAttributeName;
    NSString *const NSKernAttributeName;
    NSString *const NSStrikethroughStyleAttributeName;
    NSString *const NSUnderlineStyleAttributeName; 
    NSString *const NSStrokeColorAttributeName;
    NSString *const NSStrokeWidthAttributeName;
    NSString *const NSShadowAttributeName; 
    NSString *const NSVerticalGlyphFormAttributeName;
    
    
    
     // Create attributed string
     NSString *str = @"example for underline \nexample for font \nexample for bold \nexample for italics"; 
    
     NSMutableAttributedString *attributedString =
     [[NSMutableAttributedString alloc] initWithString:str];
    
     // Add attribute NSUnderlineStyleAttributeName 
     [attributedString addAttribute:NSUnderlineStyleAttributeName 
                       value:[NSNumber numberWithInt:NSUnderlineStyleSingle] 
                       range:NSMakeRange(12, 9)];
     [attributedString addAttribute:NSUnderlineStyleAttributeName
                       value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
                       range:NSMakeRange(12, 9)];
    
     // Set background color for entire range
     [attributedString addAttribute:NSBackgroundColorAttributeName
                       value:[UIColor yellowColor]
                       range:NSMakeRange(0, [attributedString length])];
    
    
     // Create NSMutableParagraphStyle object
     NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
     paragraph.alignment = NSTextAlignmentCenter;
    
     // Add attribute NSParagraphStyleAttributeName
     [attributedString addAttribute:NSParagraphStyleAttributeName 
                       value:paragraph
                       range:NSMakeRange(0, [attributedString length])];
    
    
    
     // Set font, notice the range is for the whole string
     UIFont *font = [UIFont fontWithName:@"Helvetica" size:18]; 
     [attributedString addAttribute:NSFontAttributeName 
                       value:font
                       range:NSMakeRange(35, 4)];
    
    
    
     // Set font, notice the range is for the whole string
     UIFont *fontBold = [UIFont fontWithName:@"Helvetica-Bold" size:18];
     [attributedString addAttribute:NSFontAttributeName 
                       value:fontBold 
                       range:NSMakeRange(53, 4)];
    
     // Set font, notice the range is for the whole string 
     UIFont *fontItalics = [UIFont fontWithName:@"Helvetica-Oblique" size:18];
     [attributedString addAttribute:NSFontAttributeName 
                       value:fontItalics
                       range:NSMakeRange(71, 7)];
    
    
    
     // Set label text to attributed string
     [self.mytextView setAttributedText:attributedString];
    

    更多信息请参考here

    【讨论】:

      【解决方案2】:

      我的扩展函数将html 转换为NSAttributedString。如果License.txt由html语法组成,则可以直接转换为NSAttributedString

      @implementation NSString (Html)
      - (NSAttributedString *)htmlToAttributeString
      {
          NSAttributedString *str = [[NSAttributedString alloc] initWithData:[self dataUsingEncoding:NSUTF8StringEncoding]
                                                                     options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                               NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                                          documentAttributes:nil
                                                                       error:nil];
          return str;
      }
      @end
      

      【讨论】:

        【解决方案3】:

        这可能会有所帮助

        self.textView.attributedText =
        [   NSAttributedString.alloc
            initWithFileURL:[ NSBundle.mainBundle URLForResource:@"License" withExtension:@"txt"  ]
            options:nil
            documentAttributes:nil
            error:err];
        

        编辑:如果每个单独的单词或字符或段落都需要格式化,那么最好使用 html 代码并使用 NSAttributedString 加载它。 谢谢

        【讨论】:

        • 我从 txt 文件中检索到普通文本,然后我想为检索到的文本设置属性字符串。可以吗?
        • 然后你使用加载html文本文件
        猜你喜欢
        • 1970-01-01
        • 2016-12-17
        • 1970-01-01
        • 1970-01-01
        • 2021-02-09
        • 2020-04-19
        • 1970-01-01
        • 2011-03-16
        相关资源
        最近更新 更多