Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add background line in multiline label using NSAttributedString in ios objective-c

I want to add background line in multiline label using NSAttributedString in objective-c as shown in image belowenter image description here

I used following code

[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

But it doesnt work perfectly and I want to implement appropriate methods how can I acheive that. Using these are not appropriate too Link1,Link2,Link3,Link4

I was able to solve using following code

NSAttributedString * title =
            [[NSAttributedString alloc] initWithString:@"I implement My test in here"
                                            attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
            [ self.viewOrderCell.labelItem setAttributedText:title];
like image 269
Nischal Hada Avatar asked Mar 16 '23 22:03

Nischal Hada


2 Answers

Below code may help to solve your problem.

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;
like image 191
Nimit Parekh Avatar answered Apr 27 '23 10:04

Nimit Parekh


You may mean Strike,iOS 6.0 and upper , UILabel supports NSAttributedString and NSMutableAttributedString

When use NSAttributedString

NSAttributedString * title =
    [[NSAttributedString alloc] initWithString:@"Your String here"
                                    attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];

Definition :

/** 
 * 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;

When use NSMutableAttributedString

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

Definition :

/**
 *
 *  @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;

Then

yourLabel.attributedText = attributeString;
like image 33
ChenYilong Avatar answered Apr 27 '23 10:04

ChenYilong