I want to change the font size of my textfield which is usually done with fontSize attribute in a text composable, however textfield composable does not come with that attribute, how do I change the font size then?
TextField(
textStyle = TextStyle(fontSize = 45.sp)
)
Related
I tried to look around but I can't find a way to
force a focus,
set the cursor to the end of text
and still be able to set the cursor to any part of the text when
pressing/clicking.
With FocusRequester the cursor is set to the start of text, but with TextFieldValue(<text>, <range>) I'm able to place the cursor at the end, the problem is that with this approach the cursor is always forced to any specified selection = TextRange(<index>)(in my case its the end using the current length of the changing value onValueChange), I have no idea how to set the cursor in any part (selection) when I press/click the TextField.
My Implementation:
var textFieldValue by remember { mutableStateOf(TextFieldValue("Some Text")) }
Row (
modifier = Modifier
.height(150.dp)
.fillMaxWidth()
.clickable {
focusRequester.requestFocus()
textFieldValue = textFieldValue.copy(selection = TextRange("Some Text".length))
}
) {
BasicTextField(
modifier = Modifier.focusRequester(focusRequester),
value = textFieldValue,
onValueChange = {
textFieldValue =
textFieldValue.copy(text = it.text, selection = TextRange(it.text.length))
},
)
}
And what I'm trying to achieve (Large text area with text set and starts from top-left), its entire part should be clickable and triggers focus for the text field, the reason why I wrapped it in a Row with a clickable modifier.
I wasn't able to achieve this with a single text field with specified height, as TextAlign doesn't have a Top-Start alignment
I have a UITextField and UILabel sitting together in a UIView as so:
and here it is in Xcode:
The label is hidden until the user enters some text into the text field, so it serves to provide a persistent "suffix" to the numeric entry. The problem is that when the user types a number into the text field, it doesn't shrink down to the size of the text, it remains at the size of the original placeholder, even though it isn't visible, as so:
Is there any way I can constrain the text field's width to be the minimum size to accommodate the user's text, and not pay attention to the invisible placeholder text's width?
Thank you
I managed to solve it myself:
Whenever the text is edited, the text field is checked to see if there is any text inside. If there is no text, the 'mg' suffix is hidden, and the placeholder is added. If there is text, the 'mg' suffix is shown and the placeholder is removed. Like so: (Swift)
func textFieldTextChanged(_ textField: UITextField) {
milligramTextField.invalidateIntrinsicContentSize()
view.layoutIfNeeded()
if textField.text == "" {
milligramSuffixLabel.text = ""
milligramTextField.placeholder = "0 mg"
} else {
milligramSuffixLabel.text = " mg"
milligramTextField.placeholder = ""
}
}
This answer was of great help.
I wanted to use SF's monospaced digits font to display an integer number in a text field by changing its font as follows:
textField.font = UIFont.monospacedDigitSystemFont(textField.font!.pointSize, weight: UIFont.Weight.semibold)
But if I set the text of the text field in a 60Hz frequency, this is the result:
The width of the text is clearly not constant for a same amount of digits so it is moving all jittery because the text field is constrained to "leading" and "trailing" of the image underneath.
Why is this the case and how to fix it?
Another truly monospaced font like "Menlo" is behaving correctly:
So it seems like setting the "adjust to fit" option of the text field is overwriting the monospaced property of the font (even if the view is big enough to contain the text!)
My temporary solution at this point is to
Set textField.adjustsFontSizeToFitWidth to false
Set the monospaced font again (as it was somehow overwritten by adjustsFontSizeToFitWidth's setter)
Begin to change the text
When finished set textField.adjustsFontSizeToFitWidth to true again in order to correctly behave on user input
This is not what I originally wanted but it works as a workaround
I have a project that uses the "adjust to fit" option and if I set the monospaced property using didSet for the label outlet, it seems to work for me in Xcode 8 Beta 3.
#IBOutlet weak var textLabel: UILabel! {
didSet {
textLabel = UIFont.monospacedDigitSystemFont(ofSize: textLabel!.pointSize, weight: UIFontWeightRegular)
}
}
In my case, I was setting the font property of a UILabel to a monospaced digit font and using attributedText to set the text. This worked in iOS 9 but broke in iOS 10. The workaround is to explicitly set the font attribute in the attributed string.
I was having the same problem and did set code worked. Updated for Swift 4:
#IBOutlet weak var textLabel: UILabel! {
didSet {
label.font = UIFont.monospacedDigitSystemFont(ofSize: textLabel!.font!.pointSize, weight: UIFont.Weight.regular)
}
}
I have a label where there is text which should be bold and with another font size. Is there any possibility to do it like the line break ("Hello \n World!") with a command or do I have to make another label for this?
Thanks!
Look at the API for NSAttributedString -- it allows you to create a string that specifies portions of the string that should be styled with specific text styles and/or fonts. The resulting object can be used instead of a plain string with UILabel (and other UI elements) by setting the label's attributedText property instead of the usual text property.
To make just the word "bold" appear in 18 point bold, try something like the following:
var label = UILabel()
let bigBoldFont = UIFont.boldSystemFontOfSize(18.0)
var attrString = NSMutableAttributedString(string: "This text is bold.")
attrString.addAttribute(kCTFontAttributeName, value: bigBoldFont, range: NSMakeRange(13, 4))
label.attributedText = attrString
The range specified determines the portion of the string to which the named attributed (in this case, the font) should be applied. And note that the parameters to NSMakeRange are the starting character position and the length of the range.
I am new to BB application. I want customized the size for the button text and label text.
I am using this code
try {
FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif");
Font appFont = alphaSansFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);
setFont(appFont);
} catch (ClassNotFoundException e) {
}
but using this it will change size for what all the field we are using within the constructor.But I need different size for different field .Like Title will be big size and other label text button text will be small size.
I am guessing that you are calling that when you are creating the screen so the setFont() changes the font for the full screen.
You can call set font on the required button or label and it will change the font only for that field
e.g button.setFont(yourFont)
Are you calling setFont() explicitly on the Field you wish to change?
just in the above try block donot declere the statement setFont(appFont), this sets the font of the whole screen.
Insead set the font by specifying which label or button you want to set the font like
LabelField lf1 = new LabelField("Testing");
lf1.setFont(appFont);
Again
ObjectChoiceField ocf1 = new ObjectChoiceField("","","");
ocf1.setFont(appFont);