Contents

Develop
2013.04.23 18:07

[ios] UILabel top alignㅎㅎ

Views 23853 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
나를 귀찮게 하는 아이폰의 api 부재
레이블에 대해서 문자의 가로정렬은 쉽게 되지만, 세로정렬은 sdk 의 속성을
눈 씻고 찾아봐도 찾을 수 없어 구걸신에게 또 구걸하여 아래와 같은 코드를 얻었다.

개념은 NSString의 sizeWithFont 메소드를 사용하여 실제로 그려질 크기를 손쉽게 구한 후에
이 값을 UILabel의 frame에 대입해주는 것이다.

아래 예제는 top 정렬 뿐 아니라 bottom 정렬도 가능한 예제이다.
- (void)setUILabel:(UILabel *)myLabel
  withMaxFrame:(CGRect)maxFrame
  withText:(NSString *)theText
  usingVerticalAlign:(int)vertAlign
{

  CGSize stringSize =
    [theText sizeWithFont:myLabel.font
      constrainedToSize:maxFrame.size
      lineBreakMode:myLabel.lineBreakMode];
 
  switch (vertAlign) {
    case 0: // vertical align = top
      myLabel.frame = CGRectMake(myLabel.frame.origin.x, 
        myLabel.frame.origin.y, 
        myLabel.frame.size.width, 
        stringSize.height);
      break;

    case 1: // vertical align = middle
      // don't do anything,
      // lines will be placed in vertical middle by default
      break;

    case 2: // vertical align = bottom
      myLabel.frame = CGRectMake(myLabel.frame.origin.x, 
        (myLabel.frame.origin.y + myLabel.frame.size.height)
         - stringSize.height, 
        myLabel.frame.size.width, 
        stringSize.height);
      break;
  }

  myLabel.text = theText;
}

참조: http://discussions.apple.com/thread.jspa?messageID=8322622

?

List of Articles
No. Category Subject Author Date Views
747 Develop [c#] 전자금융보안론 발표/설치 자료(툴바 소스코드) secret hooni 2016.05.07 0
746 Develop XE Core 1.8.18 본문 작성시 태그(html) 사라지는 버그 file hooni 2016.04.21 2112
745 Develop [io] Apple Watch, Today Extension 앱ID 설정 hooni 2016.04.20 4174
744 Develop [ios] Did UIScrollView End Scrolling? hooni 2016.04.19 8344
743 Develop [ios] How To Use UIScrollView to Scroll and Zoom Content (Using Swift) file hooni 2016.03.23 11627
742 Develop [ios] How To Use UIScrollView to Scroll and Zoom Content (Using Objective-C) file hooni 2016.03.23 4259
741 Develop [js] AngularJS 란? file hooni 2015.11.26 2189
740 Develop [php] 한글 문자열 자르기 (utf-8) hooni 2015.11.10 2786
739 Develop [c] 한글 문자열 출력 hooni 2015.11.10 3140
738 Develop [ios] NavigationController 에서 왼쪽(back) 버튼 후킹하기 hooni 2015.10.23 4696
737 Develop [ios] ViewController Push할 때 애니메이션 효과 hooni 2015.10.23 2124
736 Develop [linux] CentOS Apache 웹서버에 HTTPS 적용 hooni 2015.10.23 2094
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 71 Next
/ 71