iOS中关于UILabel的一些特殊处理

1、动态计算Label的高度

+ (CGFloat)calculateCellHeightWithMessage:(MessageDetailData *)str

{

    CGFloat contentLabelWide = [UIScreenmainScreen].bounds.size.width -3*inset -headImageWide;

    CGSize maximumContentLabelSize =CGSizeMake(contentLabelWide,FLT_MAX);

    CGFloat totalHeight =0;

    NSDictionary *attribute =@{NSFontAttributeName: [UIFontsystemFontOfSize:contentSet]};

    CGSize contentSize = [str.contentboundingRectWithSize:maximumContentLabelSizeoptions:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOriginattributes:attributecontext:nil].size;

    if (str.content.length >0) {

        totalHeight += contentSize.height;

    }

    if (totalHeight <=40) {

        return80;

    }else

    {

        //注意:算法的Bug,导致必须添加一个像素点

        return totalHeight +dateHight +headImageHight +1;

    }

}


2、修改Label的行间距

var attributedString = NSMutableAttributedString(string: message.summary)

var paragraphStyle = NSMutableParagraphStyle()

paragraphStyle.lineSpacing =3.0

paragraphStyle.lineBreakMode =NSLineBreakMode.ByTruncatingTail

attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range:NSMakeRange(0,NSString(string: message.summary).length))

contentLabel.attributedText = attributedString

3、修改Lable中某几个字的大小

var attributeString = NSMutableAttributedString(string: doctorInfo!.name! +"   " +doctorInfo!.title!)

        attributeString.addAttribute(NSFontAttributeName, value:UIFont.systemFontOfSize(22.0), range:NSMakeRange(0,NSString(string:doctorInfo!.name!).length))

        userName.attributedText = attributeString


4、修改Label中某几个字的颜色

var attributeString = NSMutableAttributedString(string: "您现在可以直接联系" +doctorInfo!.name! +"")

            attributeString.addAttribute(NSForegroundColorAttributeName, value:XRMacro.UIColorFromRGB(0x51b4bd), range:NSMakeRange(9,NSString(string:doctorInfo!.name!).length))

            titleLabel.attributedText = attributeString

5、随字体,改变Label的大小

[yAxisLabel sizeToFit];


6、随着宽度变化,改变字体大小

//总金额

    UILabel *label = [selfaddLabelInView:CGRectMake(20 ,10 + 20 +5,150,60)title:[self.myIntegraModel.totalstringValue]textColor:UIColorFromRGB(0xff9900)font:46.0fontName:4];

 label.adjustsFontSizeToFitWidth =YES;

   [view addSubview:label];

7、随着字体数量的变化,计算宽度

CGSize size = [@"0"sizeWithAttributes:[NSDictionarydictionaryWithObject:self.labelFontforKey:NSFontAttributeName]];

8、使用第三方库MLLinkLabel 处理带URL的文本

1)、处理成带下划线

let detailLabel: MLLinkLabel = cell.viewWithTag(2003)as! MLLinkLabel

let attributeStr = NSMutableAttributedString(HTML: self.scheduleModel?.remarks!)

                attributeStr.addAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0),NSForegroundColorAttributeName: XRMacro.UIColorFromRGB(0x68b1bb)], range:NSMakeRange(0, attributeStr.length))

                detailLabel.linkTextAttributes = [NSForegroundColorAttributeName:XRMacro.UIColorFromRGB(0x68b1bb)]

                detailLabel.attributedText = attributeStr

                detailLabel.didClickLinkBlock = { [weakself] (link, linkText, label) -> Void in

                    let aLink:MLLink = link asMLLink

                    if aLink.linkType ==MLLinkType.Other || aLink.linkType ==MLLinkType.URL  {

                        self!.pushToWebView(aLink.linkValue!, webType:nil)

                    }

                }


2)、处理不带

 detailLabel.text =scheduleModel?.remarks

                    detailLabel.linkTextAttributes = [NSForegroundColorAttributeName:XRMacro.UIColorFromRGB(0x68b1bb)]

                    detailLabel.didClickLinkBlock = { [weakself] (link, linkText, label) -> Void in

                        let aLink:MLLink = link asMLLink

                        XRMacro.DLog(aLink.linkValue)

                        if aLink.linkType ==MLLinkType.URL {

                            self!.pushToWebView(aLink.linkValue!, webType:nil)

                        }

                   }

9、全局替换

 NSString *content = @"test123";
    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:0 error:nil];
    content = [regularExpression stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:@"*"];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值