UI 텍스트 필드 텍스트를 수직으로 중앙에 배치하려면 어떻게 해야 합니까?
나는 단지 예를 들어 설명할 뿐입니다.UITextField텍스트가 수직으로 중심이 맞지 않는다는 것을 알아차립니다.대신 버튼 상단과 같은 높이인데, 기본값이 수직으로 중앙에 위치할 것으로 예상하기 때문에 이상하다고 생각합니다.세로로 중심을 맞추려면 어떻게 해야 합니까, 아니면 누락된 기본 설정이 있습니까?
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
신속한 사용:
textField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
이는 이전 답변에서 언급한 것과 같이 몇 가지 복잡한 요인이 있을 수 있습니다.
- 정렬하려는 항목(숫자, 문자, 대문자 또는 혼합만 해당)
- 자리 표시자
- 지우기 단추
줄 높이, 오름차순, 내림차순 등으로 인해 글꼴의 어느 점이 수직으로 중심을 잡아야 하는지에 따라 정렬하려는 항목이 중요합니다.

(출처: ilovetypography.com )
(이미지는 http://ilovetypography.com/2009/01/14/inconspicuous-vertical-metrics/ 덕분입니다)
예를 들어 숫자만 다루는 경우 표준 중앙 정렬이 올바르게 표시되지 않습니다.아래의 두 가지 차이점을 비교해 보십시오. 이 두 가지는 동일한 정렬을 사용합니다(아래 코드). 둘 중 하나는 올바르고 다른 하나는 약간 어긋나 보입니다.
여러 글자가 섞여 있는 것은 옳지 않습니다.

하지만 숫자만 맞다면 맞는 것 같습니다.

그래서 불행하게도, 시각적으로 정확하게 보이기 위해서는 약간의 시행착오와 수동 조정이 필요할 수도 있습니다.
나는 아래 코드를 UITextField의 하위 클래스에 배치했습니다.지우기 단추가 있는 것을 고려하여 슈퍼클래스 메서드를 호출합니다.
override func awakeFromNib() {
contentVerticalAlignment = UIControlContentVerticalAlignment.Center
}
override func textRectForBounds(bounds: CGRect) -> CGRect {
let boundsWithClear = super.textRectForBounds(bounds)
let delta = CGFloat(1)
return CGRect(x: boundsWithClear.minX, y: delta, width: boundsWithClear.width, height: boundsWithClear.height - delta/2)
}
override func editingRectForBounds(bounds: CGRect) -> CGRect {
let boundsWithClear = super.editingRectForBounds(bounds)
let delta = CGFloat(1)
return CGRect(x: boundsWithClear.minX, y: delta, width: boundsWithClear.width, height: boundsWithClear.height - delta/2)
}
override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
let delta = CGFloat(1)
return CGRect(x: bounds.minX, y: delta, width: bounds.width, height: bounds.height - delta/2)
}
스토리보드에서:제어 하에서 -> 필요에 따라 수직 및 수평 정렬을 변경합니다.

textField의 텍스트에 적합합니다.그러나 자리 표시자 텍스트(텍스트 필드가 비어 있는 동안 나타나는 텍스트)를 사용하려면 iOS 7에서 문제가 발생합니다.
TextField 클래스를 무시하고 해결했습니다.
- (void) drawPlaceholderInRect:(CGRect)rect
방법.
다음과 같이:
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor blueColor] setFill];
CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
[[self placeholder] drawInRect:placeholderRect withFont:self.font lineBreakMode:NSLineBreakByWordWrapping alignment:self.textAlignment];
}
iOS7 및 이전 버전에서 모두 작동합니다.
텍스트 기준선을 조정하여 문제를 해결할 수도 있습니다.
// positive: up, negative:down
// NSAttributedStringKey.baselineOffset:0
let attDic: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 16),
.baselineOffset: 0
];
textfield.placeholder = NSAttributedString(string: "....", attributes: attDic);
언급URL : https://stackoverflow.com/questions/5439099/how-do-i-vertically-center-uitextfield-text
'programing' 카테고리의 다른 글
| ASP.NET 정적 변수의 수명 (0) | 2023.06.09 |
|---|---|
| SQL Server의 기본 포트 (0) | 2023.06.09 |
| 상태가 계산에서 업데이트되지 않는 이유는 무엇입니까? (0) | 2023.06.09 |
| XML 파일을 R 데이터 프레임으로 구문 분석하는 방법은 무엇입니까? (0) | 2023.06.09 |
| 경로의 일부를 찾을 수 없습니다... bin\roslin\csc.exe (0) | 2023.06.04 |