如何在ios objective-c中使用NSAttributedString在多行标签中添加背景行

如何在ios objective-c中使用NSAttributedString在多行标签中添加背景行,第1张

概述我想在 objective-c中使用NSAttributedString在多行标签中添加背景线,如下图所示 我使用以下代码 [yourView.layer setBorderWidth:5.0];[yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"] 我想在 objective-c中使用NSAttributedString在多行标签中添加背景线,如下图所示

我使用以下代码

[yourVIEw.layer setborderWIDth:5.0];[yourVIEw.layer setbordercolor:[[UIcolor colorWithPatternImage:[UIImage imagenamed:@"DotedImage.png"]] CGcolor]];///just add image name and create image with dashed or doted drawing and add here

但它没有完美的工作,我想实现适当的方法,我怎么能实现这一点.使用这些也不合适Link1,Link2,Link3,Link4

我能够使用以下代码解决

NSAttributedString * Title =            [[NSAttributedString alloc] initWithString:@"I implement My test in here"                                            attributes:@{NsstrikethroughStyleAttributename:@(NSUnderlinestyleSingle)}];            [ self.vIEwOrderCell.labelitem setAttributedText:Title];
解决方法 你可能意味着Strike,iOS 6.0和更高版本,UILabel支持NSAttributedString和NSMutableAttributedString

使用NSAttributedString时

NSAttributedString * Title =    [[NSAttributedString alloc] initWithString:@"Your String here"                                    attributes:@{NsstrikethroughStyleAttributename:@(NSUnderlinestyleSingle)}];[label setAttributedText:Title];

定义:

/**  * Returns an NSAttributedString object initialized with a given string and attributes. * *  @param str : The string for the new attributed string. *  @param attrs : The attributes for the new attributed string. For information *  about where to find the attribute keys you can include in this dictionary,*  see the overvIEw section of this document. * *  @return     Returns an NSAttributedString object initialized with the characters of aString and the attributes of attributes. The returned object might be different from the original receiver. */- (instancetype)initWithString:(Nsstring *)str attributes:(NSDictionary *)attrs;

使用NSMutableAttributedString时

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];[attributeString addAttribute:NsstrikethroughStyleAttributename                        value:@2                        range:NSMakeRange(0,[attributeString length])];

定义:

/** * *  @param name : A string specifying the attribute name. Attribute keys can be *  supplIEd by another framework or can be custom ones you define. For *  information about where to find the system-supplIEd attribute keys,see the *  overvIEw section in NSAttributedString Class Reference.   * *  @param value : The  attribute value associated with name. * *  @param arange : The range of characters *  to which the specifIEd attribute/value pair applIEs. */- (voID)addAttribute:(Nsstring *)name value:(ID)value range:(NSRange)arange;

然后

yourLabel.attributedText = attributeString;
总结

以上是内存溢出为你收集整理的如何在ios objective-c中使用NSAttributedString在多行标签中添加背景行全部内容,希望文章能够帮你解决如何在ios objective-c中使用NSAttributedString在多行标签中添加背景行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1058599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存