Formatting UITableView Cells on iphone

I am developing an application for iphone. In the image below, I want the first name to be in normal font and the last to be bold. How can i do this? Please suggest me .. Please check this image:

alt text

Another task ... Now I think the way back, but the problem is in the first line, and the second line that you see is part of the same line. I want the first line to be in bold and the second to be in normal font. I keep this in a dictionary. So my dictionary has a key, and the value is a string of names and sections. I can not install the font. I tried to create two labels and tried to split the string according to the index and assign it to the methods that I created. But in this case, the index continues to change, as there may be a name for the contact or there can be no name.

In this case, Prinicipal should be in the normal font, and the name should be in bold

Please see the image below:

alt text

+3
source share
4 answers

Running OS 6.0 in UILabel is called the attributedText attribute, which has an NSAttributedString type (available in iOS 3.2 and later).

Here is how I use it:

  NSDictionary  *firstNameAttributes = @{ NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:18.0],
                                          NSStrokeColorAttributeName : [UIColor blackColor]};
  NSDictionary  *lastNameAttributes = @{NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:20.0],
                                        NSStrokeColorAttributeName : [UIColor blackColor]};
  NSString* first = ... first name ..;
  NSString* last = [NSString stringWithFormat:@" %@",... last name ....];
  NSMutableAttributedString * name =
    [[NSMutableAttributedString alloc] initWithString:first attributes:firstNameAttributes];
  NSMutableAttributedString * lastName =
  [[NSMutableAttributedString alloc] initWithString:last attributes:lastNameAttributes];
  [name appendAttributedString:lastName];

  [[cell textLabel] setAttributedText:name];

See also Introduction to Attribute Programming Guide .

+7
source

, , , , uilabel , . , uilabel : firstNameLabel.font = [UIFont systemFontOfSize:12]; : lastNameLabel.font = [UIFont boldSystemFontOfSize:12];.

firstNameLabel, [firstNameLabel sizeToFit], . firstNameLabel, lastNameLabel .

UILabel * firstNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,100,25)];
firstNameLabel.tag = firstNameLabelTag //This should be a constant probably
firstNameLabel.font = [UIFont systemFontOfSize:12];
firstNameLabel.text = theStringRepresentingTheFirstName;
[firstNameLabel sizeToFit];

UILabel * lastNameLabel = [[UILabel alloc] initWithFrame:
    CGRectMake(10+firstNameLabel.frame.size.width+2, 10, 100, 25)];
lastNameLabel.tag = lastNameLabelTag //This should be a constant probably
lastNameLabel.font = [UIFont boldSystemFontOfSize:12];
lastNameLabel.text = theLastNameString;.

[cell.contentView addSubview:firstNameLabel];
[cell.contentView addSubview:lastNameLabel];

, , , . , - ( ).

, , .

+5
- (IBAction)buttonPressed:(UIButton *)sender {
    NSString *title = [sender titleForState:UIControlStateNormal];
    NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];
    NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc] initWithString:plainText];
    NSDictionary *attributes = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:self.statusLabel.font.pointSize]};
    NSRange nameRange = [plainText rangeOfString:title];
    [styledText setAttributes:attributes range:nameRange];
    self.statusLabel.attributedText = styledText;

}
0
-2
source

Source: https://habr.com/ru/post/1760788/


All Articles