富文本

标签、按钮等

920 发布: 2019/10/25 07:21 本文总阅读量

标签

项目1

-(NSMutableAttributedString*)setFlagWithContentDic:(NSDictionary *)flagDic title:(NSString *)titleStr font:(CGFloat)tFont {
    
    NSString *flagStr = minstr([flagDic valueForKey:@"name"]);
    NSString *flagColStr = minstr([flagDic valueForKey:@"colour"]);
    UIColor *flagBgCol = RGB_COLOR(flagColStr, 1);
    
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.alignment = NSTextAlignmentCenter;
    paragraph.lineSpacing = 3;
    paragraph.hyphenationFactor = 1.0;
    
    NSString*itemName = [NSString stringWithFormat:@"%@ %@",titleStr,flagStr];
    NSMutableAttributedString *titleAttrStr = [[NSMutableAttributedString alloc] initWithString:itemName attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:tFont],NSParagraphStyleAttributeName: paragraph,NSForegroundColorAttributeName:UIColor.whiteColor}];
    //设置标签的背景色
    YYTextBorder *border = [YYTextBorder new];
    border.fillColor= flagBgCol;
    //调整Insets更改背景显示位置
    border.insets= UIEdgeInsetsMake(-4, -2, -1, -2);
    border.cornerRadius = 3;
    [titleAttrStr yy_setTextBackgroundBorder:border range:NSMakeRange(titleStr.length+1, flagStr.length)];
    //设置标签的文本颜色
    [titleAttrStr yy_setColor:[UIColor whiteColor] range:NSMakeRange(titleStr.length+1, flagStr.length)];
    //设置标签文本的字号
    [titleAttrStr yy_setFont:[UIFont systemFontOfSize:(tFont-4)]range:NSMakeRange(titleStr.length+1, flagStr.length)];
    //小字号偏移
    [titleAttrStr addAttribute:NSBaselineOffsetAttributeName value:@(0.66*4) range:NSMakeRange(titleStr.length+1, flagStr.length)];
    return titleAttrStr;
}

按钮+图片

项目2

-(CGFloat)getMessageHeight:(NSString *)mess andLabel:(YYLabel *)lb {
    
    NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",mess]];
    NSDictionary *userDic = [_dataDic valueForKey:@"userinfo"];
    WeakSelf;
    if ([minstr([userDic valueForKey:@"id"]) isEqual:[Config getOwnID]]) {
        //添加图片
        UIImage *image = [UIImage imageNamed:@"壁纸-详情-编辑"];
        NSMutableAttributedString *attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(15, 15) alignToFont:SYS_Font(15) alignment:(YYTextVerticalAlignment)YYTextVerticalAlignmentCenter];
        [introText appendAttributedString:attachment];
        [introText yy_setTextHighlightRange:[[introText string] rangeOfString:[attachment string]] color:[UIColor clearColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            NSLog(@"========");
            [weakSelf showEditeView];
        }];
    }
    
    introText.yy_font = SYS_Font(15);
    introText.yy_lineSpacing = 8;
    lb.attributedText = introText;
    CGSize introSize = CGSizeMake(_window_width-30, CGFLOAT_MAX);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
    lb.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    return introHeight;
}

注:使用YYLabel
上面方法添加按钮的同时计算文本高度,不涉及计算高度至lb.attributedText = introText;结束。

按钮+文字

项目3

- (void)addSeeMoreButton {
    WeakSelf;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...查看更多》"];
    YYTextHighlight *hi = [YYTextHighlight new];
    [hi setColor:[UIColor whiteColor]];
    hi.tapAction = ^(UIView *containerView,NSAttributedString *text,NSRange range, CGRect rect) {
       //
        MoreSingnatureVC *moreVC = [[MoreSingnatureVC alloc]init];
        moreVC.signatureStr = weakSelf.headerV.signatureL.text;
        moreVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:moreVC animated:YES];
        
    };
    [text yy_setColor:RGB_COLOR(@"#969696", 1) range:[text.string rangeOfString:@"..."]];
    [text yy_setColor:[UIColor whiteColor] range:[text.string rangeOfString:@"查看更多》"]];
    [text yy_setTextHighlight: hi range:[text.string rangeOfString:@"...查看更多》"]];
    text.yy_font = SYS_Font(14);
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = text;
    [seeMore sizeToFit];
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize: text.size  alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];
    _headerV.signatureL.truncationToken = truncationToken;
    
}