获取NSTextField内容以进行扩展

时间:2010-05-25 21:34:55

标签: cocoa scale nstextfield

如何使文字比例符合我给出的界限?

3 个答案:

答案 0 :(得分:21)

我过去做过类似的事情。

-(void)calcFontSizeToFitRect:(NSRect)r {
    float targetWidth = r.size.width - xMargin;
    float targetHeight = r.size.height - yMargin;

    // the strategy is to start with a small font size and go larger until I'm larger than one of the target sizes
    int i;
    for (i=minFontSize; i<maxFontSize; i++) {
        NSDictionary* attrs = [[NSDictionary alloc] initWithObjectsAndKeys:[NSFont fontWithName:currentFontName size:i], NSFontAttributeName, nil];
        NSSize strSize = [string sizeWithAttributes:attrs];
        [attrs release];
        if (strSize.width > targetWidth || strSize.height > targetHeight) break;
    }
    [self setCurrentFontSize:(i-1)];
}

字符串变量是您想要调整大小的文本。 xMargin和yMargin变量用于您想要的间距。 minFontSize和maxFontSize变量限制了你想要的小或大。

答案 1 :(得分:0)

此解决方案适用于iOS,效果很好。但是,需要注意的一件事是:如果要以编程方式进行设置,则需要使用具有宽度和高度的rect初始化NSTextfield,否则duration=c(1.5,8.9,3,6.2) total_duration=c(1.5,10.4,13.4,19.6) duration_df=data.frame(duration,total_duration) 返回0。

这也是我找到此解决方案的链接: https://medium.com/@joncardasis/dynamic-text-resizing-in-swift-3da55887beb3

bounds

答案 2 :(得分:0)

For me label.adjustsFontSizeToFitWidth = true reduces the font size. 

with...


lazy var labelContainerView: UIView =
{   let view = UIView()
    return  view.labelContainerView(view: view, label)   }()


extension UIView {
func anchor(   top: NSLayoutYAxisAnchor?,
                    left: NSLayoutXAxisAnchor?,
                    bottom: NSLayoutYAxisAnchor?,
                    right: NSLayoutXAxisAnchor?,
                    paddingTop: CGFloat,
                    paddingLeft: CGFloat,
                    paddingBottom: CGFloat,
                    paddingRight: CGFloat,
                    width: CGFloat,
                    height: CGFloat     )

    {   translatesAutoresizingMaskIntoConstraints = false

        if let top = top {   self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true   }

        if let left = left {   self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true   }

        if let bottom = bottom {   self.bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true   }

        if let right = right {   self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true   }

        if width != 0 {   widthAnchor.constraint(equalToConstant: width).isActive = true   }

        if height != 0 {   heightAnchor.constraint(equalToConstant: height).isActive = true    }
    }
}


func labelContainerView(view: UIView, _ label: UILabel) -> UIView

    {   view.addSubview(label)
        label.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        return view
    }
}