Contents

Develop
2013.04.23 18:07

[ios] UILabel top alignㅎㅎ

조회 수 22842 댓글 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. [ios] UIView에서 상위 UIViewController 가져오기

    Date2013.09.27 CategoryDevelop Byhooni Views20161
    Read More
  2. [ios] UITableView 특정 Row만 Update

    Date2014.04.08 CategoryDevelop Byhooni Views4781
    Read More
  3. [ios] UILabel top alignㅎㅎ

    Date2013.04.23 CategoryDevelop Byhooni Views22842
    Read More
  4. [ios] UIColor 지정에서 RGB define ㅎㅎ

    Date2013.04.23 CategoryDevelop Byhooni Views22916
    Read More
  5. [ios] UIButton multi-line iOS7

    Date2014.01.09 CategoryDevelop Byhooni Views11437
    Read More
  6. [ios] UIAlertView 초간단 샘플 ㅎㅎ

    Date2013.10.14 CategoryDevelop Byhooni Views46115
    Read More
  7. [ios] UDID와 UUID (디바이스의 Unique Identifier)

    Date2013.04.23 CategoryDevelop Byhooni Views27345
    Read More
  8. [ios] UDID 사용 제한에 따른 대안들

    Date2014.03.13 CategoryDevelop Byhooni Views3968
    Read More
  9. [ios] Touch ID 적용 샘플 코드 (예제)

    Date2015.02.23 CategoryDevelop Byhooni Views673
    Read More
  10. [ios] Thread Loop 내에서 UI 업데이트 방법

    Date2015.01.03 CategoryDevelop Byhooni Views853
    Read More
  11. [ios] TextField 특정 문자만 사용하도록 하기

    Date2014.06.30 CategoryDevelop Byhooni Views2684
    Read More
  12. [ios] Swift 4 String, Date, DateFormatter 예제

    Date2018.10.18 CategoryDevelop Byhooni Views1403
    Read More
Board Pagination Prev 1 ... 48 49 50 51 52 53 54 55 56 57 ... 98 Next
/ 98