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:@"*"];

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