相关疑难解决方法(0)

将文本垂直对齐到UILabel中的顶部

我有UILabel两行文字的空间.有时,当文本太短时,此文本显示在标签的垂直中心.

如何将文本垂直对齐以始终位于顶部UILabel

表示具有垂直居中文本的UILabel的图像

xcode cocoa-touch text-alignment uikit ios

2141
推荐指数
39
解决办法
64万
查看次数

UILabel - 适合文字的自动尺寸标签?

是否可以自动调整UILabel框/边界的大小以适应包含的文本?(我不在乎它是否比显示器大)

因此,如果用户输入"你好"或"我的名字真的很长,我希望它适合这个盒子",它永远不会被截断,标签会相应地"加宽"吗?

iphone objective-c uilabel ios

143
推荐指数
7
解决办法
23万
查看次数

ios动态调整大小标签

我试图在网上搜索,但没有找到我的问题的明确答案所以我来征求你的专家意见.我有一个带有2个标签的视图.第一个标签用于显示名称的缩写,2-3个字母.第二个标签显示全名.

我有的问题是,是否有办法根据给定的字体类型,大小和字符串长度动态调整标签大小?我问,因为我希望第二个标签靠近第一个标签,两者之间没有太多的空白空间,或者没有第一个标签与第二个标签重叠.

这不是一个标签的原因是因为第一个标签应该有更大的字体和不同的颜色方案,然后是第二个标签.

任何意见是极大的赞赏.

text font-size uilabel ios

28
推荐指数
5
解决办法
5万
查看次数

如何在SWIFT中将textview的高度调整为他的内容?

在我的视图控制器中,我有一个UITextView.此textview填充了一个字符串.字符串可以是短的或长的.根据字符串的长度,textview的高度必须调整.我使用故事板和自动布局.

我对textview的高度有些麻烦.

有时,高度完全适应文本.有时,不,文本在第一行被裁剪.下面,textview是黄色的.蓝色是滚动视图中的容器视图.紫色是一张照片的地方.

TITLE1 标题2

文本来自我的核心数据库,它们是字符串属性.在屏幕1和屏幕2之间,唯一改变的是字符串.
如果我在控制台中打印字符串,我有正确的文本:

my amazing new title

Another funny title for demo
Run Code Online (Sandbox Code Playgroud)

我的textview的限制:

在此输入图像描述

我不明白为什么我有2个不同的显示器.

编辑

我尝试了@Nate建议,在viewDidLoad中,我补充说:

    myTextViewTitle.text="my amazing new title"

    myTextViewTitle.setContentHuggingPriority(1000, forAxis: UILayoutConstraintAxis.Vertical)
    myTextViewTitle.setContentCompressionResistancePriority(1000, forAxis: UILayoutConstraintAxis.Vertical)
    myTextViewTitle.setTranslatesAutoresizingMaskIntoConstraints(false)  

    let views = ["new_view": myTextViewTitle]
    var constrs = NSLayoutConstraint.constraintsWithVisualFormat("|-8-[new_view]-8-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views)
    self.view.addConstraints(constrs)
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[new_view]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.myTextViewTitle.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[new_view(220@300)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
Run Code Online (Sandbox Code Playgroud)

没有结果.在界面构建器中为我的textview添加或不添加高度约束...

编辑2

我需要一个UITextView而不是UIlabel,因为后退按钮,使用排除路径.

     let exclusionPathBack:UIBezierPath = UIBezierPath(rect: CGRect(x:backButton.bounds.origin.x, y:backButton.bounds.origin.y, width:backButton.bounds.width+10, height:backButton.bounds.height))
     myTextViewTitle.textContainer.exclusionPaths=[exclusionPathBack]
Run Code Online (Sandbox Code Playgroud)

uitextview ios autolayout swift

24
推荐指数
4
解决办法
5万
查看次数

设置UIView大小以适合子视图


我有一个UIView包含两个UILabels,它们有动态高度.我想要UIView适应UILabels的大小+一些填充.现在我正在绘制标签,然后将UIView的宽度和高度限制设置为标签的组合大小.

现在我这样做:

func populateWithMessage(headline: String?, message: String) {
    // Sets text
    self.headerLabel.text = headline
    self.messageLabel.text = message

    self.headerLabel.sizeToFit()
    self.messageLabel.sizeToFit()

    self.layoutSubviews()

    // Finding the label with the greatest width
    var longestLabel: UILabel!
    if self.headerLabel.frame.width > self.messageLabel.frame.width {
       longestLabel = self.headerLabel
   } else {
       longestLabel = self.messageLabel
   }

   let combinedLabelHeight = self.headerLabel.frame.height + self.messageLabel.frame.height

   // Update the container view's constraints
   self.containerViewWidtConstraint.constant = longestLabel.frame.width + 10
   self.containerViewHeightConstraint.constant = combinedLabelHeight + 10

   self.updateConstraints()
}
Run Code Online (Sandbox Code Playgroud)

不幸的是它不起作用: - /

任何想法将不胜感激!

我正在使用Swift 2.3和Autolayout.

uiview uilabel ios autolayout swift

15
推荐指数
2
解决办法
2万
查看次数

UILabel sizeToFit和约束

有没有简单的方法可以帮助我使用内容大小动态更改相关视图的位置?

我想在列中显示几个视图,这些视图都有不同的内容.我希望它们一个接一个地放置(我使用看起来像这样的约束创建了布局)

初始布局

但每当我更改标签内容和调用时sizeToFit,系统似乎都会忽略布局.

尺寸适合调用后

目前我只对高度属性感兴趣,我知道也可以使用约束矩形,在过去我在UIView上写了很多类别来动态改变大小(我想每个人都这样做).但也许有一种我不知道的简单方法?

iphone cocoa autolayout

14
推荐指数
1
解决办法
2万
查看次数

如何调整标签大小以适合文本的长度

我在过去的QAs中搜索了解决方案,但找不到合适的QA.
有谁知道如何UILabel动态调整大小以适应文本长度?
我上传了我不想要的屏幕截图(第1行)和我想要的(第2行).
我很感激任何线索,建议或代码示例.谢谢.在此输入图像描述

objective-c uilabel ios

14
推荐指数
2
解决办法
4万
查看次数

iOS自动布局,如何使View包装内容?

查看希拉奇:

- RootView
  - ContainerView
    - Label1
    - Label2
Run Code Online (Sandbox Code Playgroud)

我希望ContainerView总是包裹Label1和Label2,在Label1或Label2内容更改时调整ContainerView的高度。

我的约束:

H: [Label1]-[Label2(==Label1)]
V: [Label1]-(>=0)
V: [Label2]-(>=0)
Run Code Online (Sandbox Code Playgroud)

也不例外,但布局不明确。

ios autolayout

8
推荐指数
2
解决办法
8820
查看次数

默认今日小组件中的布局约束冲突

我在今天为我的应用程序创建的今日小部件中观察到一个非常奇怪的关于Autolayout的行为.试图找到问题的根源我最终创建了一个简单的新Xcode项目(单视图应用程序),并添加了一个今日扩展作为新目标 - 甚至没有触及它.

当我在我的设备(iPhone 6s)上启动Today Extension时,首先发生的是在控制台中抛出布局约束冲突:

2016-05-03 18:17:22.216 TodayExtension[10183:4611907] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<_UILayoutSupportConstraint:0x15c665320 V:[_UILayoutGuide:0x15c6657b0(0)]>",
    "<_UILayoutSupportConstraint:0x15c663890 V:|-(0)-[_UILayoutGuide:0x15c6657b0]   (Names: '|':UIView:0x15c6642a0 )>",
    "<_UILayoutSupportConstraint:0x15c666010 V:[_UILayoutGuide:0x15c666380(0)]>",
    "<_UILayoutSupportConstraint:0x15c666ed0 _UILayoutGuide:0x15c666380.bottom == UIView:0x15c6642a0.bottom>",
    "<NSLayoutConstraint:0x15c666b80 V:[_UILayoutGuide:0x15c6657b0]-(NSSpace(8))-[UILabel:0x15c6617c0'Hello World']>", …
Run Code Online (Sandbox Code Playgroud)

xcode ios autolayout today-extension

6
推荐指数
1
解决办法
452
查看次数

UITableViewCell单元重用和UILabel自动布局动态调整大小

我正在使用子类UITableViewCell,我需要在使用自动布局时将UILabel的文本对齐到左上角.我意识到读取sizeToFit确实不应该与自动布局一起使用,我想避免它,并以某种方式使用约束.基本上,每次重复使用单元时都会重置标签的文本,因此在重用时,大小调整需要是动态的.

这是子类化单元格内的Lazy初始化程序标签:

- (UILabel *)commentsLabel {

    if (_commentsLabel == nil) {

         _commentsLabel = [[UILabel alloc] init];
         [_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
         _commentsLabel.numberOfLines = 0;
         _commentsLabel.lineBreakMode = NSLineBreakByWordWrapping;
         _commentsLabel.preferredMaxLayoutWidth = 100;
    }

    return _commentsLabel;
}
Run Code Online (Sandbox Code Playgroud)

标签上设置了自动布局约束(commentsLabel是添加到单元子类上的self.customView的子视图):

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|"
                                                                        options:0
                                                                        metrics:@{
                                                                                  @"locationToTopPadding":@(locationToTopPadding),
                                                                                  @"locationHeight":@(locationHeight),
                                                                                  @"locationToCommentsPadding":@(locationToCommentsPadding),
                                                                                  @"commentsToBottomPadding":@(commentsToBottomPadding)
                                                                                  }
                                                                          views:viewsDictionary]];

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|"
                                                                        options:0
                                                                        metrics:@{@"commentsWidth":@(commentsWidth)}
                                                                          views:viewsDictionary]];
Run Code Online (Sandbox Code Playgroud)

设置只是:

self.commentsLabel.preferredMaxLayoutWidth = 100;
Run Code Online (Sandbox Code Playgroud)

虽然在大多数答案中提到过,但似乎没有用.

目前有这个实施但没有看到任何结果. UILabel sizeToFit不适用于autolayout ios6

我试过的另一个答案. UILabel sizeToFit仅适用于关闭AutoLayout

我觉得我只缺少1个约束,除了上面的约束之外还可以添加,但我尝试添加的程序性约束不起作用或抛出异常.我完全在代码中工作没有xib.

ETA:尝试在现有垂直约束线内设置高度约束:

像这里建议:动态改变UILabel宽度不适用于autolayout

在上面的垂直约束线中:

-[commentsLabel(>=30@900)]-
Run Code Online (Sandbox Code Playgroud)

我已经弄乱了高度值和优先级值,没有任何改变.

ETA:取得一些进展,我认为它与标签的底部填充有关,试过这个并且一些标签正确对齐有些不是:

->=commentsToBottomPadding-
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uitableview ios autolayout

5
推荐指数
1
解决办法
4146
查看次数

如何让NSTextField的宽度适应其内容?

我一起工作Xcode5

我希望自己的宽度NSTextField适应其内容。

如果可能的话,我希望在InterfaceBuilder不产生子类的情况下进行此操作。

编辑:我正在与autolayout开!(这似乎阻止了我更改NSTextField的宽度)

xcode cocoa interface-builder nstextfield

1
推荐指数
1
解决办法
2723
查看次数