Develop
2013.04.23 18:07
[ios] UILabel top alignㅎㅎ
조회 수 22894 댓글 0
나를 귀찮게 하는 아이폰의 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
493 | Develop | [ios] URL Scheme 이용하여 앱 설치 확인 | hooni | 2014.03.10 | 4412 |
492 | Develop | [ios] UI컨트롤러 샘플코드 | hooni | 2013.08.08 | 15488 |
491 | Develop | [ios] UIWebView에서 로컬에 있는 html 파일 불러오기 | hooni | 2015.02.10 | 981 |
490 | Develop | [ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기 | hooni | 2014.01.16 | 14909 |
489 | Develop | [ios] UIWebView를 이용한 로컬 HTML 파일 표시 | hooni | 2015.01.02 | 1347 |
488 | Develop | [ios] UIWebView 쿠키 유지 | hooni | 2014.01.16 | 11757 |
487 | Develop | [ios] UIWebView 캐쉬 삭제 | hooni | 2014.04.08 | 3712 |
486 | Develop | [ios] UIView에서 상위 UIViewController 가져오기 | hooni | 2013.09.27 | 20256 |
485 | Develop | [ios] UITableView 특정 Row만 Update | hooni | 2014.04.08 | 4847 |
» | Develop | [ios] UILabel top alignㅎㅎ | hooni | 2013.04.23 | 22894 |
483 | Develop | [ios] UIColor 지정에서 RGB define ㅎㅎ | hooni | 2013.04.23 | 22969 |
482 | Develop | [ios] UIButton multi-line iOS7 | hooni | 2014.01.09 | 11465 |