富文本 簡單樣式

閒來無事,簡單整理了下富文本的一些使用方法;話不多說,直接上代碼:


控件佈局用的是 Masonry,字體用的是 迷你簡漢真廣標使用外部字體見上篇文章),

#pragma mark - 創建可變字符串的lb

- (void)buildAttributedString

{

    UIScrollView *mainScroView = [[UIScrollView alloc]init];

    mainScroView.showsVerticalScrollIndicator = NO;

    mainScroView.showsHorizontalScrollIndicator = NO;

    [self.view addSubview:mainScroView];

    [mainScroView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.right.bottom.left.mas_equalTo(0);

    }];

    

    NSString *str = @"而在梅新育看來,單一的財稅政策,也很難提振美國經濟,還需要跟接下來的福利等一系列社會改革結合起來,才能發揮最好的作用。這一點,自然也是跨國資本需要審慎考慮的。梅新育指出,很重要的一點是,“從特朗普訪華稱中國的政體適合中國人民,到近期一系列的動作來看,他偏向於減少對外部世界的干預";

    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc]initWithString:str];

    

    /**  字體大小  */

    [attributeStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:[self getMnjhzgbFont] size:15 *HEIGHT] range:NSMakeRange(0, 10)];

    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20 *HEIGHT] range:NSMakeRange(10, str.length - 10)];

    

    /**  字體顏色  */

    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 10)];

    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(10, str.length - 10)];

    

    /**  背景色  */

    [attributeStr addAttribute:NSBackgroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, 10)];

    [attributeStr addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(10, str.length - 10)];

    

    /**  刪除線  */

    [attributeStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, 10)];

    

    /**  下劃線  */

    [attributeStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(10, str.length - 10)];

    

    /**  字體的陰影  */

    NSShadow *shadow = [[NSShadow alloc]init];

    /**  陰影的清晰度  */

    shadow.shadowBlurRadius = 1;//模糊度

    shadow.shadowColor = [UIColor lightGrayColor];

    /**

     CGSizeMake(x,y)

     x爲正,爲往右偏移

     y爲正,爲往上偏移

     */

    shadow.shadowOffset = CGSizeMake(0, -3);

    /**  陰影  */

    [attributeStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(10, str.length - 10)];

    

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];

    /**  行間距  */

    paragraphStyle.lineSpacing = 20 *HEIGHT;

    [attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.length)];

    

    

    UILabel *attributeLb = [[UILabel alloc]init];

    attributeLb.attributedText = attributeStr;

    attributeLb.numberOfLines = 0;

    attributeLb.layer.borderColor = [UIColor blackColor].CGColor;

    attributeLb.layer.borderWidth = 1.0;

    [mainScroView addSubview:attributeLb];

    

    [attributeLb mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(0);

        make.top.mas_equalTo(60*HEIGHT);

        make.width.mas_equalTo(300*WIDTH);

    }];

    [attributeLb layoutIfNeeded];

    

    NSLog(@"%f",attributeLb.frame.size.height);

    mainScroView.contentSize = CGSizeMake(SCREEN_WIDTH, attributeLb.frame.size.height + 120 *HEIGHT);

    

}



效果展示:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章