Gambas3 setting Cursor position - gambas

I am using a qt5 TextEdit in Gambas3 for rich text.
Please consider the code:
Dim cursorpos As Integer
If Key.Code = Key.Left Or Key.Code = Key.Up Or Key.code = Key.Right Or Key.Code = Key.Down Or Key.Code = Key.Delete Or Key.Code = Key.Backspace Then
cursorpos = TextEdit1.Pos ' just pick the position
Print cursorpos
Else
cursorpos = TextEdit1.Pos
Print cursorpos
TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>" ' this preserves the newlines, and replaces them with a <br> for the rich text
Print "setting : ", cursorpos ' prints the correct value
TextEdit1.Pos = cursorpos ' does not work
Print "got : ", TextEdit1.Pos ' jumps to the end of the string
Endif
Now, I write :
This si a line
this is a second line
I have a typo on the first line. I use my arrow key to get there. I hit backspace twice, and remove the word si. All good. Now I expect to type in the character i, and the cursor should stay just after the character i. But as soon as the i is typed in the correct position, the cursor jumps to the end of the text.
Please help. How can I keep the cursor position in the correct place? Thank you.

your error is this line...
TextEdit1.RichText = "<font color = \"#224444\">" & Replace(TextEdit1.Text, gb.NewLine, "<br>") & "</font>"
' this preserves the newlines, and replaces them with a <br> for the rich text
actually it removes the hidden original RichText formatting
The best way to change color of the text inline without changing the internal RichText formatting is to do something like this...
TextEdit1.Format.Color = Color.Red
then text typed at that position will be in red.
So you could monitor for Delete / Backspace then set the format color to red at the current position.
gambaswiki.org/wiki/comp/gb.qt4.ext/textedit

Related

iOS - Secure Text Entry Causes Font Size And Style Of Last Character To Be Different

By default, secureTextEntry is set to YES. When the text is entered in the text field, ••••••• will be displayed. But this time. when secureTextEntry is set to NO, the font size and style of the last character of the string will always be different. Plus, there will be a big spacing between the cursor and the whole string. See screenshot below.
I found an answer from this site. http://www.jianshu.com/p/72271c023d6d
After changing the secureTextEntry property of UITextField. Just replace the string with and empty string #" ", then copy the original string back again.
Here's the code:
- (IBAction)secureSwitchAction:(id)sender
{
self.passwordTextField.secureTextEntry = !self.passwordTextField.secureTextEntry;
NSString* text = self.passwordTextField.text;
self.passwordTextField.text = #" ";
self.passwordTextField.text = text;
}

Disable Line Wrapping in a multi-line UILabel

Is there a way with a multi-line label (myLabel.numberOfLines = 0) to disable any kind of line wrapping so that if a line is too long to fit on one line of the label it just stops/kind of breaks off and doesnt wrap to the line below? So I can use "\n" to assign strings to other lines of the label. I know lines that are too long automatically wrap to the next line but I dont know if there is a no line wrap option.
So If I had a label with a line max of 10 chars per line
var firstLine : String = "This is 16 chars"
var secondLine : String = "This is too long"
myLabel.text = firstLine + secondLine
// It would look like this:
Output:
This is 16
This is to
As shown it just cuts off and doesnt wrap each line even though they dont fit
firstLine + secondLine will become 1 string This is 16 charsThis is too long, i dont think you can do something like described without code, you have to manually cut off the strings to 10 characters and add \n at the end, so it will become This is 16\nThis is to
Something like :
var string = message
let fontAttributes = [NSFontAttributeName: font]
var size = (string as NSString).sizeWithAttributes(fontAttributes)
while size.width > label.width {
string.removeAtIndex(string.endIndex.predecessor())
size = (string as NSString).sizeWithAttributes(fontAttributes)
}
string = string+"\n"
If you have desired length of your label, you can try this stupid but work method: To cut the string by yourself, use stringByPaddingToLength method.
Try below codes:
self.tempLabel.text = #"This is 16 chars fbaebfbefbefbeif";
self.tempLabel.text = [self.tempLabel.text stringByPaddingToLength:10 withString:#"" startingAtIndex:0];
See magic happens

UITableView blank space not working?

so I was trying to align the text in UITableView using.
cell.textLabel?.text = sometext
I have two part in sometext first part is words, second part is number, such as "apple 45" "pear 23", "banana 34"so when they are showing in tableview cell, I want the left side of the words align with each other and the left side the number align with each other. and I can not post a picture here.
so according to the first part word length I added some blank space in the string by appending
let appStr = String(count: 22-cnt, repeatedValue: ( " " as Character))
print("append string is" + appStr + "end")
nameHere = name + appStr + number
I printed out to console in the program and it works fine, but when showing in the simulator it is not aligned.
Don't attempt to align numbers using spaces. Set the label's attributedText, not its text, and use the fact that an NSAttributedString can have tab stops to perform the alignment.

UILabel lineBreakMode, break at specific character

Is there a way to break the text in a UILabel at a specific character say ";" ??
I don't want it to be broken with Word Wrap or character Wrap.
Sure, just replace all the occurrences of ";" with ";\n" before you show the string.
There is another way which will work in limited circumstances. You can replace your normal spaces (\U+0020) with non-breaking spaces (\U+00A0). This will allow you to limit the number of places your string breaks. For example if you had a string like;
I have a string with two sentences. I want it to preserve the sentences.
By carefully using non-breaking spaces you can get it to break like this;
I have a string with two sentences.
I want it to preserve the sentences.
HOW:
For Strings in InterfaceBuilder:
Go to System Preferences -> Keyboard -> Input Sources -> +
Select the category 'Others' and the keyboard 'Unicode Hex Input'
Select 'Add'
Make sure 'Show Input method in menu bar' is selected.
CLose System Preferences
Go back to XCode and find your string
Using the keyboard menu in your menu bar, select the Unicode keyboard
In your string select a space. Type Option+00a0. A space will appear when you've completed the 4 digit sequence. That space is a non-breaking space. Repeat for all spaces you need to.
For programmatic strings you can just add \U00A0 as appropriate.
You can use (\r) instead of newline (\n) to create a line break.
Set numberOfLines to 0 to allow for any number of lines.
yourLabel.numberOfLines = 0;
Like With in your case just replace ; with ;\n
NSString *string = #"This; is a; NSString";
string = [string stringByReplacingOccurrencesOfString:#";"
withString:#";\n"];
You can't break line using ; this character. if you want to break line then replace this character with \n character.
label.text=[label.text stringByReplacingOccurrencesOfString:#";" withString:#"\n"];
And make
label.numberOfLines = 0.
And Update the label frame
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode];
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, labelSize.height);
You can do with combination of newline character and line break.Check the following code,
self.testLabel.text = #"abc;\nabc;";
self.testLabel.numberOfLines = 0;
CGSize labelSize = [self.testLabel.text sizeWithFont:self.testLabel.font
constrainedToSize:self.testLabel.frame.size
lineBreakMode:self.testLabel.lineBreakMode];
self.testLabel.frame = CGRectMake(
self.testLabel.frame.origin.x, self.testLabel.frame.origin.y,
labelSize.width, labelSize.height);

iPhone UILabel - non breaking space

Is there a way to use non breaking spaces in UILabel text?
For example, I have label with 2 lines and line breaking mode set to word wrap. The content for this label is read from database, where it's stored as a string. Now sometimes my text in label looks like that:
lorem ipsum some text
1
but I want to display it like that:
lorem ipsum some
text 1
so basicly, I need to force non breaking space between 'text' and '1'.
I've found some solution here, but I think it could work when the text is entered in source code file. In my case the text is in database.
Any suggestions?
Use the no-break space (\u00a0) ex: #"hello**\u00a0**world!"
post.text = [postText stringByAppendingString: #"1\u00a0hour\u00a0ago."];
U+00A0 / no-break space / Common Separator, space
from: http://en.wikipedia.org/wiki/Whitespace_character
For Swift:
let sentence = "Barcelona, Real Madryt, Juventus Turyn, Bayern Monachium"
let sentencewithnbsp = String(map(sentence.generate()) {
$0 == " " ? "\u{00a0}" : $0
})
In Swift 4 I had to use all caps: \U00A0
Example:
lorem ipsum some\U00A0text 1
Update Feb 2020 from the comments. Thanks to #corban:
In the Localizable.strings file it still needs to be \U00A0 - in code
you have to use \u{00a0}
If you want to get this to work in a .strings file, you can type OPTION + SPACE instead of a regular space.
.strings files are usually UTF-8 encoded, so if you open it in Hex view, you will see "C2 A0" in place of the special space character.
In Inspector set number of lines for Label as 3 or 4 What ever you require Then the Content will be displayed in multiple lines.
You may need to implement a custom word-wrapping function.
// pseudo-code
instring;
get len(instring)
if len > textedit.width*charFontSize
for (textedit.width*charFontSize ) // cycle through string
insert `\n` into inString at shortest whitespace
or something like that.
I don't think there's a simple way to do this with UILabel. Of course one way to achieve the same effect is to manually insert a "\n" before "text" in your example. Another option is to use a UIWebView with static HTML instead of the UILabel, so you can use an actual &nbsp.
Here's a Swift extension to fix an orphan in a string by replacing the last space with a non-breaking space:
extension String {
var replacingLastSpace: String {
if let lastSpace = range(of: " ", options: .backwards, locale: .current) {
return replacingCharacters(in: lastSpace, with: "\u{00a0}")
}
return self
}
}
Although note as of iOS 11, UILabels solve this problem automatically.
If this is not going to happen often, you can do this:
NSString *string = #"lorem ipsum some \ntext 1";
label.text = string;
You can dynamically generate where you put the \n using character counts, word counts etc...

Resources