2010-08-20 8 views
3

iphone 앱을 개발 중입니다. 아래 이미지에서 나는 이름을 일반 글꼴로, 성을 굵게 표시하고 싶습니다. 어떻게 할 수 있습니까? 저를 제안하십시오 ..이 이미지를 확인하시기 바랍니다 :iphone의 UITableView 셀 포맷하기

alt text

또 다른 questio..Now 나는 반대 방법을 생각하지만, 여기에 문제는 당신이 동일한 문자열의 일부입니다 볼 수있는 첫 번째 줄과 두 번째 줄입니다. 첫 번째 줄을 굵게 표시하고 두 번째 줄을 보통 글씨로 바꾸기를 원합니다. 나는 이것을 사전에 보관하고있다. 그래서 내 사전에는 키가 있고 그 값은 이름과 부서의 문자열입니다. 글꼴을 설정할 수 없습니다. 두 개의 레이블을 만들려고 시도하고 색인에 따라 문자열을 분할하고 내가 만든 레이블에 할당하려고했습니다. 그러나이 경우 색인은 연락처의 이름이 있거나 이름이 없기 때문에 계속 변경됩니다.

이 경우 Prinicipal 굵게

에 있어야 일반 글꼴 이름에 있어야합니다 아래 이미지 참조하십시오

alt text

답변

5

이름 문자열을 표시하는 레이블은 서식과 스타일을 정의하므로 다른 스타일을 사용하려면 원하는 각 스타일마다 다른 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]; 

이름 문자열을 분리하는 경우에는 매우 제한적일 수 있습니다. 첫 번째 공간에서 나뉘었고 첫 번째 문자열이 마지막 이름이라고 가정합니다 (첫 번째 그림 에서처럼).

원칙적인 경우는 비슷하지만 제시 할 스타일마다 레이블이 필요합니다.

7

OS를 6.0으로 시작하면 attributedText라는 UILabel에 NSAttributedString 유형의 속성이 있습니다 (iOS 3.2 이상에서 사용 가능).

여기에 내가 그것을 사용하고 방법은 다음과 같습니다

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]; 

Introduction to Attributed String Programming Guide를 참조하십시오.

0
- (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; 

}