Contents

Develop
2013.04.23 18:07

[ios] UILabel top alignㅎㅎ

조회 수 22846 댓글 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. 이어서 작업할 내용~

    Date2013.11.21 CategoryDevelop Byhooni Views0
    Read More
  2. [c#] 전자금융보안론 발표/설치 자료(툴바 소스코드)

    Date2016.05.07 CategoryDevelop Byhooni Views0
    Read More
  3. [ios] 스터디 자료 (from 종길M)

    Date2013.06.04 CategoryDevelop Byhooni Views0
    Read More
  4. [js] 2048 예쁘게 만들고 있는거.. ㅋㄷ

    Date2015.01.30 CategoryDevelop Byhooni Views0
    Read More
  5. [ios] 패스(path) 앱 메뉴 스타일

    Date2013.06.11 CategoryDevelop Byhooni Views0
    Read More
  6. 아이 훌레시 작업중 ㅋㅋ

    Date2013.08.09 CategoryDevelop Byhooni Views0
    Read More
  7. [ios] IB 없이 개발하기 (html)

    Date2013.05.31 CategoryDevelop Byhooni Views0
    Read More
  8. [ios] 최신 UI 모음.. (나중에 정리할 것)

    Date2013.08.09 CategoryDevelop Byhooni Views0
    Read More
  9. [iphone] PerfectlyClear 보정솔루션 최신..

    Date2013.05.28 CategoryDevelop Byhooni Views0
    Read More
  10. 사이버보안실무 수업 메모

    Date2017.03.23 CategoryDevelop Byhooni Views0
    Read More
  11. DDay Memo 1.9.4 소스코드

    Date2015.10.03 CategoryDevelop Byhooni Views0
    Read More
  12. [ios] APNS 샘플 코드..

    Date2013.06.27 CategoryDevelop Byhooni Views0
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 71 Next
/ 71