【问题标题】:How to add background line in multiline label using NSAttributedString in ios objective-c如何在ios Objective-c中使用NSAttributedString在多行标签中添加背景线
【发布时间】:2023-04-06 12:26:02
【问题描述】:

我想在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];

【问题讨论】:

  • 检查这个:*.com/questions/13133014/…,我想你的意思是Strike
  • 我会检查并在第二天以任何方式回复你提前谢谢

标签: ios objective-c uilabel cashapelayer


【解决方案1】:

以下代码可能有助于解决您的问题。

NSString* textValue = @"Pages you view in iOS Multiple lines of text in";
NSMutableAttributedString *strikeString = [[NSMutableAttributedString alloc] initWithString:textValue];
[strikeString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [strikeString length])];
[descriptionLbl  setAttributedText:strikeString];
descriptionLbl.numberOfLines = 0;

【讨论】:

  • 好的,我会尝试实现并检查它
  • 感谢您的努力,我实现了您的代码,效果非常棒
  • @nischalhada 非常感谢
  • 对不起老兄,我接受了下一个答案,因为它已经解决了我为你投赞成票的任何方式
  • 你对这个问题有什么想法吗*.com/questions/31049551/…
【解决方案2】:

你的意思可能是Strike,iOS 6.0及以上,UILabel支持NSAttributedStringNSMutableAttributedString

当使用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;

【讨论】:

  • 好的,我会尝试实现并检查它
  • 感谢您的努力,您的代码也很好,我为您的努力点赞
  • 你对这个问题有什么想法吗*.com/questions/31158404/…
【解决方案3】:
NSDictionary *attrDict = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0],NSForegroundColorAttributeName : UIColorFromRGB(0X2846B3)};

NSMutableAttributedString *title =[[NSMutableAttributedString alloc] initWithString:hotleUrl attributes: attrDict];

[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,[title length])];
[urlBttn setAttributedTitle:title forState:UIControlStateNormal];

【讨论】:

  • 请检查。我认为您的问题可以通过此代码解决。
最近更新 更多