Contents

Develop
2013.04.23 18:07

[ios] UILabel top alignㅎㅎ

조회 수 22838 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
나를 귀찮게 하는 아이폰의 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

?

  1. [js] 자바스크립트(Javascript) 코드를 동적으로 삽입하는 방법..

    Date2013.04.23 CategoryDevelop Byhooni Views22873
    Read More
  2. [ios] UILabel top alignㅎㅎ

    Date2013.04.23 CategoryDevelop Byhooni Views22838
    Read More
  3. 밸런싱 로봇.. 최종.. (관련 논문도 첨부) ㅋㅋ

    Date2013.04.23 CategoryDevelop Byhooni Views22816
    Read More
  4. [vb] 문자열에서 태그 제거함수 (Visual Basic)

    Date2013.04.23 CategoryDevelop Byhooni Views22617
    Read More
  5. [iphone] 화면 전환 Portrait & Landscape Mode

    Date2013.04.23 CategoryDevelop Byhooni Views22306
    Read More
  6. [doc] Json Framework 설치와 사용

    Date2013.04.23 CategoryDevelop Byhooni Views21914
    Read More
  7. [c] 재미있는 코딩..

    Date2003.04.23 CategoryDevelop Byhooni Views21314
    Read More
  8. 프로그래밍에서 foo, bar 함수의 유래

    Date2013.06.25 CategoryDevelop Byhooni Views21238
    Read More
  9. [c] 포인터 학습용 예제 소스 코드

    Date2013.04.23 CategoryDevelop Byhooni Views21025
    Read More
  10. [c] 이진트리/트리 순회법 코드(전위/중위/후위)

    Date2015.07.02 CategoryDevelop Byhooni Views20914
    Read More
  11. git 브런치 배우기 (링크)

    Date2013.07.09 CategoryDevelop Byhooni Views20571
    Read More
  12. [c++] 디렉토리 내의 파일 찾기 FindFirstFile() 함수

    Date2013.04.23 CategoryDevelop Byhooni Views20564
    Read More
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 71 Next
/ 71