가변 크기 이미지에 AutoLayout의 최적 텍스트 배치
10316 단어 AutoLayoutSwift
제목.
이런 명품.png에 사용자가 입력한 텍스트를 설정하는 방법
실장 의 비결
AutoLayout의
Multiplier
를 사용하여 이미지 크기에서 백분율 위치를 지정할 수 있습니다.지정 방법
먼저 UIlabel 구성 준비
adjustsFontSizeToFitWidth
true
minimumScaleFactor
에서 최소 글꼴 크기 지정var nameLabel: UILabel = {
let lb = UILabel()
lb.text = "きんにくひろし"
lb.numberOfLines = 1
lb.font = UIFont.boldSystemFont(ofSize: 48)
lb.adjustsFontSizeToFitWidth = true
lb.minimumScaleFactor = 8 / lb.font.pointSize
return lb
}()
AutoLayout 지정
translatesAutoresizingMaskIntoConstraints
: false
NSLayoutConstraint
로 Layout 진행multiplier
모원소의 백분율 상대 위치를 지정하는 데 사용됩니다.※ 이때
left margin
는 부요소right margin
를 대상으로 지정할 수 있습니다.left margin
를 대상으로 지정하면 시작점 0이기 때문에 레이아웃이 작동하지 않습니다.※ 위아래 좌우 여백을 지정하지 않으면 텍스트는adjust가 되지 않습니다
nameLabel.translatesAutoresizingMaskIntoConstraints = false
// 名前LabelのAutoLayout
imageView.addConstraints([
// top
NSLayoutConstraint(
item: nameLabel,
attribute: .top,
relatedBy: .equal,
toItem: imageView,
attribute: .bottom,
multiplier: 0.65,
constant: 0
),
// bottom
NSLayoutConstraint(
item: nameLabel,
attribute: .bottom,
relatedBy: .equal,
toItem: imageView,
attribute: .bottom,
multiplier: 0.85,
constant: 0
),
// left
NSLayoutConstraint(
item: nameLabel,
attribute: .left,
relatedBy: .equal,
toItem: imageView,
attribute: .right,
multiplier: 0.23,
constant: 0
),
// right
NSLayoutConstraint(
item: nameLabel,
attribute: .right,
relatedBy: .equal,
toItem: imageView,
attribute: .right,
multiplier: 0.7,
constant: 0
)
])
완성
기초적인 이미지 크기가 변해도 텍스트는 예쁘게 수납되어 있다
Custom View를 사용하면 여러 화면에서 사용하기 편리합니다.
Landscape
Portrait
Multiplier의 값은 어떻게 결정합니까?
Zeplin으로 쉽게 떼어낼 수 있어요.
Alt
를 누르면 확인하고 싶은margin 근처로 커서를 옮길 때 부모 요소에서margin을% 표시할 수 있기 때문에 그것을 사용하십시오.
(스케치도 가능. 살짝 건드리면% 표시 불가)
사은품: SnapKit를 사용한 실크
41줄->6줄만 적으면 편해요.
// 名前LabelのAutoLayout
nameLabel.snp.makeConstraints { (make) in
make.top.equalTo(imageView.snp.bottom).multipliedBy(0.65)
make.bottom.equalTo(imageView.snp.bottom).multipliedBy(0.85)
make.left.equalTo(imageView.snp.right).multipliedBy(0.23)
make.right.equalTo(imageView.snp.right).multipliedBy(0.7)
}
Reference
이 문제에 관하여(가변 크기 이미지에 AutoLayout의 최적 텍스트 배치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/hirothings/items/2c3fe2b0d5b244f8ffc2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)